package com.cari.oa.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cari.oa.dao.BaseDao;
import com.cari.oa.dao.SoftModuleDao;
import com.cari.oa.domain.SoftModuleEntity;
import com.cari.oa.util.StrUtils;

@Service
@Transactional(rollbackFor = Exception.class)
public class SoftModuleService extends BaseService<SoftModuleEntity> {
	
	@Resource
	private SoftModuleDao softModuleDao;

	@Override
	public BaseDao<SoftModuleEntity> getDao() {
		return softModuleDao;
	}
	/**
    * Title: getPage
    * Description: 分页查询
    * @author    hjx 
    * @date       2013-1-29 
    * @param name
    * @param pId
    * @param status
    * @param page
    * @param size
    * @return
    */
	@Transactional(readOnly = true)
    public List<SoftModuleEntity> getPage(String name, String pId, String status, String projectId, int page, int size) {
    	return softModuleDao.getPage(name, pId, status, projectId,page, size );
    }
    
	/**
    * Title: getCount
    * Description:查询的记录数
    * @author    hjx 
    * @date       2013-1-29 
    * @param name
    * @param pId
    * @param status
    * @return
    */
	@Transactional(readOnly = true)
    public long getCount(String name, String pId, String status, String projectId) {
    	return softModuleDao.getCount(name, pId, status, projectId);
    }
	
	/**
	* Title: existByEnname
	* Description:判断模块名称是否存在
	* @author    hjx 
	* @date       2013-1-29 
	* @param name
	* @return
	*/
	public boolean existByEnname(String name , String projectId) {
		
		return softModuleDao.existByEnname(name,projectId);
	}
	
	/**
	* Title: getModuleByProjectId
	* Description:
	* @author    hjx 
	* @date      2013-3-6 
	* @param projectId
	* @return
	*/
	public List<SoftModuleEntity> getModuleByProjectId(String projectId) {
		
		 return softModuleDao.getModuleByProjectId(projectId);
	}
	
	/**
	* Title: getTree
	* Description: 根据节点获得树
	* @author    hjx 
	* @date       2013-1-24 
	* @param isAll  等于 true 表示全部节点的树，false 表示去除叶子节点的树
	* @return
	*/
    @Transactional(readOnly = true)
	public List<String> getTree(boolean isAll,String projectId) {
    	List<String> levString = new ArrayList<String>();
    	List<SoftModuleEntity> treeNodes = softModuleDao.getModuleByProjectId(projectId);
    	if(treeNodes == null || treeNodes.size() == 0)
    		return null;
    	if(isAll)
    	    treeNodes = getTree(treeNodes); 
    	else
    		treeNodes = getTree(softModuleDao.getPage(null, null, null,projectId, 0, 0)); 
		for(SoftModuleEntity currentNode : treeNodes)
		{
			levString.add(currentNode.toTreeString());
		}
		return levString;
	}
    
	/**
	* Title: getTree
	* Description:获取树型列表
	* @author    hjx 
	* @date       2013-1-23 
	* @param src
	* @return
	*/
	private  List<SoftModuleEntity> getTree(List<SoftModuleEntity> src)
	{
		List<SoftModuleEntity> parents = new ArrayList<SoftModuleEntity>();  //存储一级节点
		List<SoftModuleEntity> last = new ArrayList<SoftModuleEntity>();     //存储非一级节点
		for (SoftModuleEntity ent : src)
		{
			if (StrUtils.isEmpty(ent.getpId()))                        //一级节点的PNO为null
			{
				SoftModuleEntity result = ent;
				result.setChildren(new ArrayList<SoftModuleEntity>());
				parents.add(result);
			}else
			{
				last.add(ent);
			}
		}
		buildTree(parents, last);
		return parents;
	}
	
	/**
	* Title: buildTree
	* Description:递归构建树
	* @author    hjx 
	* @date       2013-1-23 
	* @param parents 一级节点（pNo不等于null）
	* @param childList 非一级节点（中间节点和子节点）
	*/
	private  void buildTree(List<SoftModuleEntity> parents, List<SoftModuleEntity> childList)
	{
		try{
			List<SoftModuleEntity> record = new ArrayList<SoftModuleEntity>();
			for (Iterator<SoftModuleEntity> parentIt = parents.iterator(); parentIt.hasNext();)
			{
				SoftModuleEntity currentParent = parentIt.next();
				if (currentParent.getId() != null)
				{
					for (Iterator<SoftModuleEntity> childIt = childList.iterator(); childIt.hasNext();)
					{
	
						SoftModuleEntity currentChild = childIt.next();
						if (currentParent.getId().equals(currentChild.getpId()))
						{
							if (null == currentParent.getChildren())
							{
						    	currentParent.setChildren(new ArrayList<SoftModuleEntity>());
							}
							currentParent.getChildren().add(currentChild);
							record.add(currentChild);
							childIt.remove();
						}
					}
				}
			}
			if (record.size() == 0 || childList.size() == 0)
			{
				return;
			}
			else
				buildTree(record, childList);
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public List<String> getByPId(String id) {
		return softModuleDao.getByPId(id);
	}
}
